package edu.uwosh.cs342.group1;

import edu.uwosh.cs342.group1.database.DbCloudHelper;
import edu.uwosh.cs342.group1.storage.CloudUser;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.EditText;

public class AddUserActivity extends Activity {

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.adduser);
    }
    
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
			Intent i = new Intent(AddUserActivity.this, LogInActivity.class);
			AddUserActivity.this.startActivity(i);
			AddUserActivity.this.finish();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    /**
     * Action when the user pressed the Ok button on login
     * Authenticates the user, indicates whether they are valid or not
     * @param v
     */
    public void okButton(View v) {
		String username = ((EditText)findViewById(R.id.usernameBox)).getText().toString();
		String password = ((EditText)findViewById(R.id.password1Box)).getText().toString();
		String password2 = ((EditText)findViewById(R.id.password2Box)).getText().toString();
		CloudUser user = new CloudUser(username, password);
		//Change to more logical
		if(password.equals(password2)) {
			boolean success = new DbCloudHelper().AddUser(user);
			if (success) {
				createUserSuccess();
			} else {
				usernameErrorPrompt();
			}
		} else {
			loginErrorPrompt();
			((EditText)findViewById(R.id.password1Box)).setText("");
			((EditText)findViewById(R.id.password2Box)).setText("");
		}
	 }
	 
    /**
     * Action when the user presses the cancel button on login
     * Backs out of the application
     * @param v
     */
	 public void cancelButton(View v) {
		Intent i = new Intent(AddUserActivity.this, LogInActivity.class);
		AddUserActivity.this.startActivity(i);
		AddUserActivity.this.finish();
	 }
	 
	 /**
	  * Shows when the passwords do not match
	  */
	 public void createUserSuccess() {
		AlertDialog.Builder alert = new AlertDialog.Builder(this);
		alert.setTitle("User created");
		alert.setMessage("User has been created");
		alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int whichButton) {
				Intent i = new Intent(AddUserActivity.this, LogInActivity.class);
				AddUserActivity.this.startActivity(i);
				AddUserActivity.this.finish();
			}
		});
		alert.show();
	 }
	 
	 /**
	  * Shows when the passwords do not match
	  */
	 public void loginErrorPrompt() {
		AlertDialog.Builder alert = new AlertDialog.Builder(this);
		alert.setTitle("Log In Mismatch");
		alert.setMessage("Passwords do not match");
		alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int whichButton) {
				dialog.cancel();
			}
		});
		alert.show();
	 }
	 
	 /**
	  * Shows when the user exists in the cloud database
	  */
	 public void usernameErrorPrompt() {
		AlertDialog.Builder alert = new AlertDialog.Builder(this);
		alert.setTitle("Log In Mismatch");
		alert.setMessage("Username is already used.");
		alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int whichButton) {
				dialog.cancel();
			}
		});
		alert.show();
	 }
}
